package 力扣.数组;

public class 斐波那契数509 {
    public static void main(String[] args) {
        int fib = fib2(2);
        System.out.println(fib);
    }
    public static int fib(int n) {
          if (n == 0 ){
             return 0;
          }
          if (n == 1){
              return 1;
          }
          return fib(n-1)+fib(n-2);
    }
    public static int fib2(int n) {
        if (n<2){
            return n;
        }
        int pre1=0;
        int pre2=1;
        for (int i = 2; i <=n ; i++) {
            int p=pre1+pre2;
            pre1=pre2;
            pre2=p;
        }
          return pre2;
    }
}
